﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.SqlClient;
using System.Data;

namespace Timetabling.DAL 
{
    public class GenericDAO<T> : IGenericDAO<T>
    {
        private string connectionString;

        private string entityName;
        private IList<string> fieldsNames = new List<string>();


        public GenericDAO(string connectionString)
        {
            this.connectionString = connectionString;

            entityName = typeof(T).Name; //nos da el nombre de una clase es parecido al getType
            fieldsNames = typeof(T).GetProperties()
                                   .Where(p=>string.Compare(p.PropertyType().Namespace,"system",true) == 0) //si al comparar es 0 es qu son iguales, true es para q ignore mayusculas y minusculas
                                   .Select(p => p.Name).ToList();
            //esto era antes de tener que filtrar xa q no meta listas:   fieldsNames = typeof(T).GetProperties().Select(p => p.Name).ToList(); //p es por cada elemento coge solo el nombre p.Nombre y guardalo en la lista
            /*Esto es lo mismo que la linea anterior:
             
               fieldsNames = typeof(T).GetProperties();
               forech(PropertyInfo info in propertyList)
               {
             *      fieldName.Add(info.Name);
               }
             
             */
        }
        
        public void Add(T t)
        {

            string command =
               string.Format("Insert {0} into {1} Values (@{2})",
               string.Join(",", fieldsNames), 
               entityName,
                string.Join(",@", fieldsNames) //mete entre cada parametro la ,@
               /* Asi era antes de filtrar para quitar los listados.
               string.Format("Insert into {0} Values (@{1})", 
               entityName,
                string.Join(",@", fieldsNames) //mete entre cada parametro la ,@
                */
               );

            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(command, sqlConnection);
                foreach (var field in fieldsNames)
                {
                    sqlCommand.Parameters.Add(
                        string.Format("@{0}",field),
                        typeof(T).GetProperty(field).GetValue(t, null)
                        ); 
                }
                
                try
                {
                    sqlConnection.Open();
                    sqlCommand.ExecuteNonQuery();
                }
                finally
                {
                    if (sqlConnection.State != ConnectionState.Closed)
                    {
                        sqlConnection.Close();
                    }
                }
            }
        }

        public void Update(T t)
        {
            throw new NotImplementedException();
        }

        public T Get(Guid id)
        {
            throw new NotImplementedException();
        }

        public IList<T> GetAll()
        {
            throw new NotImplementedException();
        }

        public int Count()
        {
            throw new NotImplementedException();
        }

        public void Remove(T t)
        {
            throw new NotImplementedException();
        }
    }
}
